<template>
  <div>
    <div class="query-form">
      <el-form :model="tableList" inline ref="queryFormRef">
        <el-form-item label="相机名称" prop="menuName">
          <el-input
            placeholder="请输入相机名称"
            v-model="tableList.menuName"
          ></el-input>
        </el-form-item>
        <el-form-item label="相机状态" prop="menuState">
          <el-select placeholder="请选择相机状态" v-model="tableList.menuState">
            <el-option label="正常" :value="1"></el-option>
            <el-option label="停用" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="handleReset('queryFormRef')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="base-table">
      <div class="menu-btn">
        <el-button type="primary" @click="handleAdd(1)" v-has="'menu-create'">新增</el-button>
      </div>
      <el-table :data="menuList" style="width: 100%">
        <el-table-column
          v-for="(item, index) in columns"
          :key="index"
          :label="item.label"
          :prop="item.prop"
          :formatter="item.formatter"
        />
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.row._id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      v-model="menuAddDialog"
      :title="action == 'add' ? '相机新增' : '相机编辑'"
      :before-close="handleClose"
    >
      <el-form
        :model="dialogForm"
        label-width="100px"
        ref="dialogFormRef"
        :rules="rules"
      >
        <el-form-item prop="parentId" label="父级相机">
          <el-cascader
            v-model="dialogForm.parentId"
            :options="menuList"
            :props="{ checkStrictly: true, value: '_id', label: 'menuName' }"
            clearable
            placeholder="请选择父级相机"
          />
          <span>不选则默认新增一级相机</span>
        </el-form-item>
        <el-form-item prop="menuType" label="相机类型">
          <el-radio-group
            v-model="dialogForm.menuType"
            @change="handleMenuTypeChange"
          >
            <el-radio :label="1">相机</el-radio>
            <el-radio :label="2">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="menuName" label="相机名称" required>
          <el-input
            v-model="dialogForm.menuName"
            placeholder="请输入相机名称"
          ></el-input>
        </el-form-item>
        <el-form-item v-show="dialogMode == 1" prop="icon" label="相机图标">
          <el-input
            v-model="dialogForm.icon"
            placeholder="请输入相机图标"
          ></el-input>
        </el-form-item>
        <el-form-item v-show="dialogMode == 1" prop="path" label="路由地址">
          <el-input
            v-model="dialogForm.path"
            placeholder="请输入路由地址"
          ></el-input>
        </el-form-item>
        <el-form-item prop="menuCode" label="权限标识">
          <el-input
            v-model="dialogForm.menuCode"
            placeholder="请输入权限标识"
          ></el-input>
        </el-form-item>
        <el-form-item
          v-show="dialogMode == 1"
          prop="component"
          label="组件路径"
        >
          <el-input
            v-model="dialogForm.component"
            placeholder="请输入组件路径"
          ></el-input>
        </el-form-item>
        <el-form-item
          v-show="dialogMode == 1"
          prop="menuState"
          label="相机状态"
        >
          <el-radio-group v-model="dialogForm.menuState">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">停用</el-radio>
            <el-radio :label="3">运行中</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleCancle">取消</el-button>
          <el-button type="primary" @click="handleConfirm">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import utils from "../../utils/utils";
export default {
  name: "Menu",
  data() {
    return {
      tableList: {
        menuState: 1,
      },
      menuList: [],
      columns: [
        { label: "相机id", prop: "_id" },
        { label: "相机编号", prop: "cameraId" },
        { label: "云盘机", prop: "yunpan" },
        {
          label: "相机状态",
          prop: "state",
          formatter(row, col, value) {
            return {
              1: "已绑定",
              2: "未绑定"
            }[value];
          },
        },
        {
          label: "新增时间",
          prop: "createTime",
          formatter(row, col, value) {
            return utils.formateDate(new Date(value));
          },
        },
      ],
      menuAddDialog: false,
      action: "add",
      dialogForm: {
        menuType: 1,
        menuState: 1,
      },
      dialogMode: 1, //相机:1,按钮:2
      rules: {
        menuName: [
          { required: true, message: "请输入相机名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在2-8个字符", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.getCameraList();
  },
  methods: {
    handleQuery() {
      this.getCameraList();
    },
    handleAdd(type, row) {
      this.action = "add";
      this.menuAddDialog = true;
      if (type == 2) {
        this.dialogForm.parentId = [...row.parentId, row._id].filter(
          (item) => item
        );
      }
    },
    handleReset(form) {
      this.$refs[form].resetFields();
    },
    async handleDelete(id) {
      let res = await this.$api.menusOperate({ _id: id, action: "delete" });
      this.$message.success("删除成功");
      this.getCameraList();
    },
    handleEdit(row) {
      this.action = "edit";
      this.menuAddDialog = true;
      this.$nextTick(() => {
        Object.assign(this.dialogForm, row);
      });
      this.getCameraList();
    },
    handleCancle() {
      this.handleReset("dialogFormRef");
      this.menuAddDialog = false;
    },
    handleClose() {
      this.handleReset("dialogFormRef");
      this.menuAddDialog = false;
    },

    async getCameraList() {
      let res = await this.$api.getCameraList(this.tableList);
      this.menuList = res;
    },
    handleMenuTypeChange(type) {
      this.dialogMode = type;
    },
    handleConfirm() {
      this.$refs.dialogFormRef.validate(async (valid) => {
        if (valid) {
          let { action, dialogForm } = this;
          let params = { ...dialogForm, action };
          let res = await this.$api.menusOperate(params);
          this.menuAddDialog = false;
          this.$message.success("操作成功");
          this.handleReset("dialogFormRef");
          this.getCameraList();
        }

      });
    },
  },
};
</script>

<style lang="scss">
.menu-btn {
  padding: 20px;
}
</style>